<template>
	<view>

		<u--form labelPosition='top' labelWidth='700rpx' :model="formState" name="basic" layout="horizontal"
			:labelCol="{span:7}" :wrapperCol="{span: 13, offset: 0}">
			<u-collapse :value="['1','2']">
				<!-- <u-collapse-item name="1" title="点位信息" icon="https://project.gxrunxiang.com/cdn/cell.png"> -->
					<u-form-item label="线路名称" name="Name" :required="true">
						<u--input v-model="formState.Name" />
					</u-form-item>
					<u-form-item label="铺设方式" @click="showpicker=true" :required="true" name="BuildType">
						<u--input v-model="formState.BuildType" />
					</u-form-item>
					<u-form-item label="序号" :required="true" name="Idx" :rules="[{ required: true, message: '请输入值' }]">
						<u--input type='number' allow-half v-model="formState.Idx" :min="0" />
					</u-form-item>
					<u-form-item label="到机房的距离" :required="true" name="Length" :rules="[{ required: true, message: '请输入值' }]">
						<u--input type='number' allow-half v-model="formState.Length" :min="0" />
					</u-form-item>
					<u-form-item label="运维类型" @click="showSheet=true" :required="true" name="Type"  prop="Type">
						<u--input v-model="formState.Type" disabledColor="#ffffff" disabled />
					</u-form-item>
					<u-form-item label="备注" name="Remark" :rules="[{ required: true, message: '请输入值' }]">
						<u--input allow-half v-model="formState.Remark" :min="0" />
					</u-form-item>
					<u-form-item label="坐标" name="Latlng" :rules="[{ required: true, message: '请输入值' }]">
						<u--input allow-half v-model="formState.Latlng" :min="0" />
					</u-form-item>
					<u-action-sheet :show="showSheet" :actions="TypeData" title="请选择类型" @close="showSheet = false"
						@select="typeSelect">
					</u-action-sheet>
				</u-collapse-item>

				<u-collapse-item name="2" title="现场照片" icon="https://project.gxrunxiang.com/cdn/image.png">
					<u-upload :sizeType="['compressed']" :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
						:maxCount="10"></u-upload>
				</u-collapse-item>
			</u-collapse>
		</u--form>
		<br>
		<p>
		<u-button type="primary" text="保存" @click="save"></u-button>
		</p>
		<u-notify ref="uNotify"></u-notify>
		<u-picker :show="showpicker" @confirm="confirm" @close="showpicker=false" ref="uPicker" :columns="columns" @change="changeHandler"></u-picker>
	</view>
</template>

<script>
	import {
		baseUrl
	} from '@/common/config.js'
	import {
		mapGetters
	} from 'vuex'

	import {
		getlinebyid,
		addline,
		guid,
	} from '@/common/api.js'
	export default {
		data() {
			return {
				userinfo:{},
				formState: {Latlng:''},
				fileList1: [],
				buildid: null,
				id: null,
				notifyData: {
					message: '保存成功',
					type: 'success',
					color: '#ffffff',
					bgColor: '',
					fontSize: 15,
					duration: 3000,
					safeAreaInsetTop: false
				},
				columns: [
					['支持资源', '管道、直埋'],
					['落地光缆交接箱', '壁挂光缆交接箱', '落地光站箱', '壁挂光站箱', '光缆分线箱', '落地分配器箱', '壁挂分配器箱', '新建电杆', '原有电杆', '新设7/2.6拉线', '原有拉线', '顶杆']
				],
				columnData: [
					['落地光缆交接箱', '壁挂光缆交接箱', '落地光站箱', '壁挂光站箱', '光缆分线箱', '落地分配器箱', '壁挂分配器箱', '新建电杆', '原有电杆', '新设7/2.6拉线', '原有拉线', '顶杆'],
					['大号手井', '中号手井', '小号手井', '检查井', '新建直埋光缆', '普通接头标石', '新增接头标石', '7孔梅花管', '波纹管']
				],
				TypeData: [{name:'抢修'},{name:'巡检'},{name:'整治'}],
				showpicker: false,
				showSheet: false
			}
		},
		onLoad(option) {
			this.userinfo = JSON.parse(uni.getStorageSync("$userinfo"))
			this.formState.Id = guid() //option.id
			this.formState.Latlng = option.latlng
			console.log("latlng"+this.formState.Latlng)
			if (option.id)
				this.getdate(option.id)
			try {
				let temppoint = JSON.parse(uni.getStorageSync("temppoint"))
				this.formState.Name = temppoint.Name
				this.formState.BuildType = temppoint.BuildType
				this.formState.Idx = parseFloat(temppoint.Idx) + 0.01
			} catch (e) {}
		},
		computed: {
			...mapGetters(['user']),
		},
		methods: {
			
			confirm(e)
			{
				// console.log('confirm', e);
				this.formState.BuildType=e.value[1]
				this.showpicker=false
			},
			changeHandler(e) {
				const {
					columnIndex,
					index,
					// 微信小程序无法将picker实例传出来，只能通过ref操作
					picker = this.$refs.uPicker
				} = e
				if (columnIndex === 0) {
					picker.setColumnValues(1, this.columnData[index])
				}
			},
			save() {
				// this.formState.Buildid=this.buildid
				uni.setStorageSync("temppoint", JSON.stringify(this.formState))
				addline(this.formState).then(res => {
					this.$refs.uNotify.show(this.notifyData)
					// this.goBack()
					uni.navigateBack({
						delta: 1
					})
				})

			},
			goBack() {
				let pages = getCurrentPages(); // 当前页面　　
				console.log("pages" + pages)
				let beforePage = pages[pages.length - 2]; // 上一页
				uni.navigateBack({
					delta: 1,
					// success: function() {
					// 	// beforePage.myMapView.mounted();
					// 	console.log(beforePage.route)
					// 	console.log(beforePage.getdata)
					// 	// beforePage.getdata(); // 执行上一页的onLoad方法
					// }
				});
			},
			getdate(id) {
				getbuilddailybyid(id).then(res => {
					this.formState = res.data
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				// console.log(this.user.Id)
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: `${baseUrl}/SysFile/upload?outid=${this.formState.Id||this.userinfo.Id}&type=现场照片`,
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						header: {
							"Authorization": `Bearer ${this.$store.getters.$token}`
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
			typeSelect(e) {
				this.formState.Type = e.name
			},
		}
	}
</script>

<style>

</style>
